package cn.tedu.sp04.controller;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.entity.Order;
import cn.tedu.sp01.entity.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        Order order = orderService.getOrder(orderId);
        return JsonResult.ok().data(order);
    }

    @GetMapping("/add")
    public JsonResult<?> add(){
        Order order = new Order();
        order.setId("56e");
        order.setUser(new User(8,null,null));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1,"商品1",1),
                new Item(2,"商品2",7),
                new Item(3,"商品3",5),
                new Item(4,"商品4",5),
                new Item(5,"商品5",7),
        }));
        orderService.addOrder(order);
        return JsonResult.ok().msg("添加订单成功");
    }
    /*
    * 不设置的话控制台每次会收到浏览器的两次请求，一次为图标请求，
    * 浏览器自动发送图标请求，若服务器有设置图标，返回给浏览器在网页显示。
    * 若没有设置请求路径“/favicon.ico”，请求会发送到用户请求的浏览器同一路径，所以控制台会显示两次请求。*/
    @GetMapping("/favicon.ico")
    public void ico(){
    }
}
